package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImp implements ShoppingCartService {
    @Resource
    ShoppingCartMapper shoppingCartMapper;
    @Resource
    DishMapper dishMapper;
    @Resource
    SetmealMapper setmealMapper;

    @Override
    public void save(ShoppingCartDTO s) {
        //查询购物车看是否存在已有的菜品
        ShoppingCart shoppingCart =
                ShoppingCart.builder().userId(BaseContext.getCurrentId())
                        .setmealId(s.getSetmealId())
                        .dishId(s.getDishId())
                        .dishFlavor(s.getDishFlavor())
                        .build();
        shoppingCart.setUserId(BaseContext.getCurrentId());

        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        if (list != null && !list.isEmpty()) {
            ShoppingCart shoppingCart1 = list.get(0);
            shoppingCart1.setNumber(shoppingCart1.getNumber()+1);
            shoppingCartMapper.updateById(shoppingCart1);
        }else{
            //新增到购物车
            if(s.getDishId()!=null){
                //添加的时菜品
                Long dishId = s.getDishId();
                Dish byId = dishMapper.getById(dishId);
                shoppingCart.setNumber(1);
                shoppingCart.setName(byId.getName());
                shoppingCart.setImage(byId.getImage());
                shoppingCart.setAmount(byId.getPrice());
                shoppingCart.setCreateTime(LocalDateTime.now());
                shoppingCartMapper.insert(shoppingCart);
            } else if (s.getSetmealId()!=null) {
                //添加套餐
                Long setmealId = s.getSetmealId();
                Setmeal byId = setmealMapper.getById(setmealId);
                shoppingCart.setNumber(1);
                shoppingCart.setName(byId.getName());
                shoppingCart.setImage(byId.getImage());
                shoppingCart.setAmount(byId.getPrice());
                shoppingCart.setCreateTime(LocalDateTime.now());
                shoppingCartMapper.insert(shoppingCart);
            }
        }
    }
    /**
     * 通过用户id查询购物车
     * */
    @Override
    public List<ShoppingCart> list() {
        return  shoppingCartMapper.selectByUserId(BaseContext.getCurrentId());
    }

    @Override
    public void clean() {
        shoppingCartMapper.deleteByUserId(BaseContext.getCurrentId());
    }

    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart4 = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart4);
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart4);
        ShoppingCart shoppingCart = list.get(0);
        if(shoppingCart!=null){
            if(shoppingCart.getNumber()>1){
                shoppingCart.setNumber(shoppingCart.getNumber()-1);
                shoppingCartMapper.updateById(shoppingCart);
            }else {
                shoppingCartMapper.deleteById(shoppingCart.getId());
            }
        }
    }
}
